<template>
  <div class="from">
    <el-dialog :title="obj.isadd?'添加':'修改'" :visible.sync="obj.isshow" @closed="closed">
      {{form}}
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="菜单名称">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单">
          <el-select v-model="form.pid" placeholder="请选择" @change="type">
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option
              :label="item.title"
              :value="item.id"
              v-for="(item,index) in arr"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="菜单类型">
          <el-radio-group v-model="form.type" disabled>
            <el-radio :label="1">目录</el-radio>
            <el-radio :label="2">菜单</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="菜单图标" v-if="this.form.pid==0">
          <el-select v-model="form.icon" placeholder="请选择">
            <el-option :value="item" v-for="(item,index) in icon" :key="index">
              <i :class="item"></i>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单地址" v-else>
          <el-select v-model="form.url" placeholder="请选择">
            <el-option
              :label="item.meta.title"
              :value="'/'+item.path"
              v-for="(item,index) in contents"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <!-- <el-switch v-model="form.status"></el-switch> -->
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="add">{{obj.isadd?'添加':'修改'}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { contents } from "../../../router/index.js";
import {
  reqMenuadd,
  reqMenuList,
  reqMenuinfo,
  reqMenuedit
} from "../../../request/api.js";
export default {
  props: ["obj"],
  data() {
    return {
      contents: contents,
      arr: [],
      //   oldfrom:[],
      form: {
        title: "",
        pid: "",
        type: "",
        icon: "",
        url: "",
        status: ""
      },
      icon: [
        "el-icon-picture-outline",
        "el-icon-camera",
        "el-icon-picture-outline-round",
        "el-icon-video-camera",
        "el-icon-bell",
        "el-icon-circle-check",
        "el-icon-help",
        "el-icon-goods",
        "el-icon-warning-outline"
      ],
      formLabelWidth: "120px"
    };
  },
  methods: {
    // 取消
    cancle() {
      this.obj.isshow = false;
    },
    closed() {
      if (!this.obj.isadd) {
        this.form = {
          title: "",
          pid: "",
          type: "",
          icon: "",
          url: "",
          status: ""
        };
      }
    },
    // 确定
    add() {
      if (this.obj.isadd) {
        reqMenuadd(this.form).then(res => {});
        this.form = {
          title: "",
          pid: "",
          type: "",
          icon: "",
          url: "",
          status: ""
        };
      } else {
        reqMenuedit(this.form).then(res => {});
      }
      this.cancle();
      this.$emit("refresh");
    },
    // 改类型
    type() {
      this.form.type = this.form.pid == 0 ? 1 : 2;
    },
    addarr() {
      reqMenuList().then(res => {
        this.arr = res.data.list;
      });
    },
    change(id) {
      reqMenuinfo({ id: id }).then(res => {
        res.data.list.id = id;
        this.form = res.data.list;
      });
    }
  },
  mounted() {}
};
</script>

<style>
.from {
  width: 500px;
  margin-top: 20px;
}
</style>